// pages/whiteNoise/whiteNoise.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "白噪音",
    isTransform: true,
    isBack: true,
    ischeck: true,
    musicitems: [{
        text: '雨声',
        icon: '../../icon/rain.png',
        check: '',
        url: 'https://7469-timemechine-6gk33n8ja79a3c4a-1300403844.tcb.qcloud.la/music/%E9%9B%A8%E5%A3%B0.mp3?sign=e19dfc847da19682dec46145e91e635f&t=1617768034'
      },
      {
        text: '篝火',
        icon: '../../icon/fire.png',
        check: '',
        url: 'https://7469-timemechine-6gk33n8ja79a3c4a-1300403844.tcb.qcloud.la/music/%E7%AF%9D%E7%81%AB.wav?sign=93fccb8338e71961ebd1899f519cb1c6&t=1617768063'
      },
      {
        text: '海浪',
        icon: '../../icon/lang.png',
        check: '',
        url: 'https://7469-timemechine-6gk33n8ja79a3c4a-1300403844.tcb.qcloud.la/music/%E6%B5%B7%E6%B5%AA.wav?sign=a78cf64f47ef485ec9ddaa63d3ebadfc&t=1617768082'
      },
      {
        text: '鸟鸣',
        icon: '../../icon/bird.png',
        check: '',
        url: 'https://7469-timemechine-6gk33n8ja79a3c4a-1300403844.tcb.qcloud.la/music/%E9%B8%9F%E9%B8%A3.wav?sign=23ef52178b9de580a8ee3aaa06f13eed&t=1617768119'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onHide: function () {
    bgMusic.stop();
  },
  startMusic(e) {
    let that = this;
    const bgMusic = wx.getBackgroundAudioManager();
    let i = e.currentTarget.id;
    let music = "musicitems[" + i + "].check";
    let musics = that.data.musicitems;
    let checkmusic = '';
    //判断当前点击是否不为空
    if (musics[i].check != '') {
      bgMusic.pause();
      bgMusic.onPause(() => {
        // 不为空设置为空 并return
        that.setData({
          [music]: ''
        })
      })
      return;
    }
    //如果为空 先将所有变为空
    for (var j = 0; j < musics.length; j++) {
      checkmusic = "musicitems[" + j + "].check";
      if (musics[j].check != '') {
        that.setData({
          [checkmusic]: ''
        })
      }
    }
    bgMusic.src = musics[i].url;
    bgMusic.title = musics[i].text;
    bgMusic.play();
    bgMusic.onPlay(() => {
      //在设置当前点击为空
      that.setData({
        [music]: 'ischeck'
      })
    })
  }
})